# 账号信息
account_info = [
    {'ID':'2020','name':'wangfang','passwd':'123456','balance':5000},
    {'ID':'2019','name':'wujing','passwd':'111111','balance':2198},
    {'ID':'2050','name':'liyun','passwd':'666666','balance':790}
]

# 查询余额
def show_balance(id):
    for account_dict in account_info:
        if id == account_dict['ID']:
            # print(account_dict)
            return account_dict['balance']

# 存款
def save_money(id, money):
    for account_dict in account_info:
        if id == account_dict['ID']:
            # print(account_dict)
            account_dict['balance'] = account_dict['balance'] + money
            return

# 取款
def draw_money(id,money):
    '''

    :param id:
    :param money:
    :return: False余额不足
    '''
    for account_dict in account_info:
        if id == account_dict['ID']:

            if int(account_dict['balance']) >= money:
                account_dict['balance'] = account_dict['balance'] - money
                return True
            else:
                #print(account_dict)
                return False


# 检查账号
def check_id(id):
    for account_dict in account_info:
        if account_dict['ID'] == id:
            return True
        else:
            return False

# 核对账户密码
def check_passwd(id, passwd):
    for account_dict in account_info:
        if account_dict['passwd'] == passwd:
            return True
        else:
            return False


# 主程序开始
n_id = 0
n_pwd = 0
while True:
    print('='*12,'欢迎来到自助存取款机系统', '='*12)

    # 输入账号, 输错3次， 退出系统
    for n_id in range(1,4):
        id = input('请输入账号:')
        if check_id(id) == False:
            if n_id < 4:
                n_id = n_id + 1
                input('     账号错误{}，请按回车键重新输入！'.format(n_id-1))
        else:
            break
    if n_id == 4:
        print('*' * 12, '欢迎下次再来！', '*' * 20)
        break

    # 输入密码，输错3次， 退出系统
    for n_pwd in range(1, 4):
        passwd = input('请输入密码：')
        if check_passwd(id,passwd) == False:
            if n_pwd < 4:
                n_pwd = n_pwd + 1
                input('     密码错误{}，请按回车键重新输入！'.format(n_pwd-1))
        else:
                    #n_pwd = 5
            break

        if n_pwd == 4:
            break

    if n_id == 4 or n_pwd == 4:
        print('*'*12,'欢迎下次再来！', '*'*20)
        break

    while True:
        # 选择操作
        print('请选择以下操作：')
        print('1、查询')
        print('2、存款')
        print('3、取款')
        print('0、退出')
        key = input('请输入对应的选择：')

        if key == '1':
            print('余额：', show_balance(id))
        elif key == '2':
            mon = int(input('请输入存入金额：'))
            save_money(id, mon)
            print('当前余额：', show_balance(id))
        elif key == '3':
            while True:
                mon = int(input('请输入取款金额：'))
                if draw_money(id, mon) == False:
                    print('余额不足，当前余额为：{}, 请按回车键重新输入取款金额！'.format(show_balance(id)))
                else:
                    break

            print('当前余额：', show_balance(id))
        elif key == '0':
            print('*' * 12, '欢迎下次再来！', '*' * 20)
            break

    break
